package com.example.demo.Controller.admin;

import com.example.demo.Mapper.GsdtMapper;
import com.example.demo.Service.GsdtService;
import com.example.demo.Service.GsdtServiceImpl;
import com.example.demo.pojo.Gsdt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Random;

@Controller
public class wechatController {
    @Autowired
    private GsdtServiceImpl mapperl;
    @RequestMapping("/admin/wechat/{id}/input")
    public String editweChat(@PathVariable Integer id, Model model){
        if (mapperl.selectGsdtById(id)!=null){
            model.addAttribute("wechat",mapperl.selectGsdtById(id));
            return "admin/wechat";
        }else {
            return "redirect:/";
        }

    }
    @GetMapping("/admin/weeidit")
    public String edit(Gsdt gsdt){
        if (gsdt.getYid() == null){
            gsdt.setYid(new Random().nextInt(999999)+100000);
            mapperl.InsertGsdt(gsdt);
        }else {
            mapperl.UpdateGsdt(gsdt.getYid(),gsdt);
        }
        return "redirect:/admin/blogs";
    }
    @GetMapping("/admin/wechat/{id}/delete")
    public String deletewe(@PathVariable Integer id){
        mapperl.DeleteGsdt(id);
        return "redirect:/admin/blogs";
    }
    @GetMapping("/admin/wechat/inputs")
    public String InsertWechat(Model model){
        model.addAttribute("wechat",new Gsdt());
        return "admin/wechat";
    }
}
